// mouse.js
import { ref, computed } from 'vue'
import { ElButton, ElMessage, ElMessageBox } from "element-plus";
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs';
// 按照惯例，组合式函数名以“use”开头 [{table:{aaaa},form:{},detail:{}}]
export function useCURD() {
    // 总数据配置的集合 
    const sumOption = ref([])
    // 表格配置的 json
    const tableOption = computed(() => {
        let option = []
        for (let i = 0; i < sumOption.value.length; i++) {
            option.push({ ...sumOption.value[i], ...sumOption.value[i].table })
        }
        return option
    })
    // 表单配置的JSON
    const formOption = computed(() => {
        let option = []
        for (let i = 0; i < sumOption.value.length; i++) {
            if (sumOption.value[i].form) {
                option.push({ component: 'input', ...sumOption.value[i], ...sumOption.value[i].form })
            } else {
                option.push({ ...sumOption.value[i], value: '', component: 'input' })
            }
        }
        return option
    })
    // 详情配置的JSON
    const detailOption = computed(() => {
        let option = []
        for (let i = 0; i < sumOption.value.length; i++) {
            if (sumOption.value[i].detail) {
                option.push({ ...sumOption.value[i], ...sumOption.value[i].detail })
            } else {
                option.push({ ...sumOption.value[i] })
            }
        }
        return option
    })
    // 表格数据
    const dataList = ref([])
    // 总的数据条数
    const totle = ref(0)
    // 当前页码 和每页显示条数
    const page = ref({ nowPage: 1, pageSize: 10 })

    let ListAPI = null
    // 获取列表的方法
    async function getList(API) {
        if (API) {
            ListAPI = API
        }
        let res = await ListAPI()
        console.log(res, 49)
        dataList.value = res.list ?? []
        totle.value = res.totle ?? 0
    }
    // 删除 
    async function delRow(delAPI) {
        ElMessageBox.confirm(
            "是否确定删除？",
            "提示",
            {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
            }
        )
            .then(() => {
                delAPI().then(e => {
                    getList(ListAPI)
                })
            })
            .catch(() => {
                ElMessage({
                    type: "提示",
                    message: "已取消",
                });
            });
    }



    return { getList, delRow, page, tableOption, formOption, detailOption, sumOption, dataList }
}